**** Tables C1, C2 and C3


********************************************************************************

* 				The gender gap in MI subjects, year 1 performance, Table C2

********************************************************************************

clear
use "${data}cleaned_data_y3.dta"
do "${blocks}construct_new_variables"

replace home=. if DomicileCategory=="0"

reg ns_std_perc_y1 i.type i.Course_NatSci_split  i.ApplyCollege if EEMP == 1, vce(robust)
onesided 2.type
estadd local course_fe "\checkmark"
estadd local apply_college_fe "\checkmark"
eststo r1

reg ns_std_perc_y1 i.type i.Course_NatSci_split  i.ApplyCollege if EEMP == 1 & home==1, vce(robust)
estadd local course_fe "\checkmark"
estadd local apply_college_fe "\checkmark"
eststo r2

esttab r1 r2 using "${tables}domestic_gender_test.tex", cells(b(pval(pval) fmt(2)) se(par fmt(2))) keep(2.type) collabels(none) ///
scalars("course_fe Subject FE" ///
"apply_college_fe Application college FE") ///
eqlabels(none) label varwidth(25) nonumbers  posthead("& Full sample & Domestic students only \\" "& (1) & (2) \\ \midrule") nonumbers mlabels(none) se(%9.2f) ///
gaps booktabs nonotes ///
replace
*/
	
********************************************************************************

*					Sample and post-enrolment attrition, Tables C1 and C3

********************************************************************************	


clear
use "${data}merged_tripos.dta"
	
	keep if Course != "PBS"
	keep if Course != "Other"

	gen home=1 if DomicileCategory=="Home"
	replace home=0 if DomicileCategory~="Home" & DomicileCategory~="0"
	
	drop if ApplyYear>=2017 

	
********************************************************************************

*									Sample

********************************************************************************	

	tempname statistics
	tempfile stats
	postfile `statistics' ///
    str100 statistic count share using "`stats'", replace

	su ApplyYear 
	local count = r(N)
	
	post `statistics' ("Applied to Cambridge")  (`count') (.) 
	
	replace Offers=0 if Offers==.
	
	su ApplyYear if Offers==1
	local count = r(N)
	
	su Offers
	local offers = r(mean)*100
	
	post `statistics' ("Recieved offers")  (`count') (`offers') 

	replace Acceptances=0 if Offers==1 & Acceptances==.
	
	su ApplyYear if Acceptances==1
	local count = r(N)
	
	su Acceptances
	local acceptances = r(mean)*100
	
	post `statistics' ("Enrolled")  (`count') (`acceptances')

********************************************************************************
 
*									Attrition 1

********************************************************************************	

	keep if Acceptances == 1	
	
********************************************************************************

*									Year 1 subject change

********************************************************************************	

	gen matching_program_y1 = 0
	replace matching_program_y1 = 1 if Course == "Economics" & CourseCatalogLongDesc_y1 == "Economics Tripos, Part I"
	replace matching_program_y1 = 1 if Course == "Engineering" & CourseCatalogLongDesc_y1 == "Engineering Tripos, Part IA"
	replace matching_program_y1 = 1 if Course == "Law" & CourseCatalogLongDesc_y1 == "Law Tripos, Part IA"
	replace matching_program_y1 = 1 if Course == "Mathematics" & CourseCatalogLongDesc_y1 == "Mathematical Tripos, Part IA"
	replace matching_program_y1 = 1 if Course == "Medicine" & CourseCatalogLongDesc_y1 == "Medical and Veterinary Sciences Tripos, Part IA"
* from 2018
	replace matching_program_y1 = 1 if Course == "Medicine" & CourseCatalogLongDesc_y1 == "Medical Sciences Tripos, Part IA"
	replace matching_program_y1 = 1 if Course == "Natural Sciences" & CourseCatalogLongDesc_y1 == "Natural Sciences Tripos, Part IA"

********************************************************************************

*									Year 2  subject change

********************************************************************************

	gen matching_program_y2 = 0
	replace matching_program_y2 = 1 if Course == "Economics" & CourseCatalogLongDesc_y2 == "Economics Tripos, Part IIA"
	replace matching_program_y2 = 1 if Course == "Engineering" & CourseCatalogLongDesc_y2 == "Engineering Tripos, Part IB"
	replace matching_program_y2 = 1 if Course == "Law" & CourseCatalogLongDesc_y2 == "Law Tripos, Part IB"
	replace matching_program_y2 = 1 if Course == "Mathematics" & CourseCatalogLongDesc_y2 == "Mathematical Tripos, Part IB"
	replace matching_program_y2 = 1 if Course == "Medicine" & CourseCatalogLongDesc_y2 == "Medical and Veterinary Sciences Tripos, Part IB"
	replace matching_program_y2 = 1 if Course == "Natural Sciences" & CourseCatalogLongDesc_y2 == "Natural Sciences Tripos, Part IB"


********************************************************************************

*									Year 3  subject change

********************************************************************************

	gen matching_program_y3 = 0
	replace matching_program_y3 = 1 if Course == "Economics" & SubjectArea_y3 == "ECT3"
	replace matching_program_y3 = 1 if Course == "Engineering" & SubjectArea_y3 == "EGT2" | SubjectArea_y3 == "MET2"  
	replace matching_program_y3 = 1 if Course == "Law" & SubjectArea_y3 == "LWT2"
	replace matching_program_y3 = 1 if Course == "Mathematics" & SubjectArea_y3 == "MAT2"

	replace matching_program_y3 = 1 if Course == "Medicine" & ///
	CourseCatalogLongDesc_y3 == "Natural Sciences Tripos, Part II: Biochemistry" | ///
	CourseCatalogLongDesc_y3 == "Natural Sciences Tripos, Part II: Biological and Biomedical Sciences" | ///
	CourseCatalogLongDesc_y3 == "Natural Sciences Tripos, Part II: Genetics" | ///
	CourseCatalogLongDesc_y3 == "Natural Sciences Tripos, Part II: History and Philosophy of Science" | ///
	CourseCatalogLongDesc_y3 == "Natural Sciences Tripos, Part II: Neuroscience" | ///
	CourseCatalogLongDesc_y3 == "Natural Sciences Tripos, Part II: Pathology" | ///
	CourseCatalogLongDesc_y3 == "Natural Sciences Tripos, Part II: Pharmacology" | ///
	CourseCatalogLongDesc_y3 == "Natural Sciences Tripos, Part II: Physiology and Psychology" | ///
	CourseCatalogLongDesc_y3 == "Natural Sciences Tripos, Part II: Physiology, Development and Neuroscience" | ///
	CourseCatalogLongDesc_y3 == "Natural Sciences Tripos, Part II: Psychology" | ///
	CourseCatalogLongDesc_y3 == "Natural Sciences Tripos, Part II: Psychology, Neuroscience, and Behaviour" | ///
	CourseCatalogLongDesc_y3 == "Natural Sciences Tripos, Part II: Zoology" 
	
	replace matching_program_y3 = 1 if Course == "Natural Sciences" & regexm(SubjectArea_y3, "NST2")==1
		

gen noexam=0
foreach x of numlist 1 2 3 {
	replace noexam=1 if TermDescription_y`x'==""
	replace noexam=1 if DataExclusions_y`x'== 1 | DataExclusions_y`x' == 2
}

replace noexam=1 if Total_y3==. & OverallPaperMarkOutOf_y3==.
replace noexam=1 if Total_y3==0 & OverallPaperMarkOutOf_y3==0
replace noexam=1 if AtypicalOutOf_y2 == 1	

gen changed=1 if matching_program_y1 == 0 | matching_program_y2 == 0 |matching_program_y3 == 0
replace changed=0 if matching_program_y1 == 1 &  matching_program_y2 == 1 &matching_program_y3 == 1

gen attrit=0
replace attrit=1 if changed==1 | noexam==1

gen attrit1=0
replace attrit1=1 if (TermDescription_y1=="" | DataExclusions_y1== 1 | DataExclusions_y1 == 2 | matching_program_y1 == 0)

	su ApplyYear if attrit1==0
	local count = r(N)
	
	su attrit1 
	local end_y1 = 100-r(mean)*100
	
	post `statistics' ("Exams in Year 1")  (`count') (`end_y1')
	
gen attrit23=0
foreach x of numlist 2 3 {
	replace attrit23=1 if TermDescription_y`x'==""
	replace attrit23=1 if DataExclusions_y`x'== 1 | DataExclusions_y`x' == 2
}

replace attrit23=1 if Total_y3==. & OverallPaperMarkOutOf_y3==.
replace attrit23=1 if Total_y3==0 & OverallPaperMarkOutOf_y3==0
replace attrit23=1 if AtypicalOutOf_y2 == 1
replace attrit23=1 if matching_program_y2 == 0 | matching_program_y3 == 0


	su ApplyYear if attrit23==0 & attrit1==0
	local count = r(N)
	
	su attrit23 if attrit1==0
	local end_y23 = 100-r(mean)*100
	
	post `statistics' ("Exams in Year 3 (final sample)")  (`count') (`end_y23')

postclose `statistics'	


********************************************************************************

* 								Attrition 2

********************************************************************************

drop home
do "${blocks}construct_new_variables"
replace home=. if DomicileCategory=="0"

***** All subjects

* Directly admitted females
	
tempname attrition
tempfile attrited
postfile `attrition' ///
    str100 attrition count_all attrit count_att change count_att23 y1mark using "`attrited'", replace

	
	post `attrition' ("\textit{All subjects}")  (.) (.) (.) (.) (.) (.)
	
	
su attrit if type==1
local count_all = r(N)
local attrit = r(mean)*100
		
su changed if type==1 & attrit==1
local count_att = r(N)
local change = r(mean)*100

su  ns_std_perc_y1 if type==1 & attrit==1
local count_att23 =r(N)
local y1mark = r(mean)*100

	post `attrition' ("\quad Directly admitted female")  (`count_all') (`attrit') (`count_att') (`change') (`count_att23') (`y1mark')

* Pooled males

su attrit if type==2
local count_all = r(N)
local attrit = r(mean)*100
		
su changed if type==2 & attrit==1
local count_att = r(N)
local change = r(mean)*100

su  ns_std_perc_y1 if type==2 & attrit==1
local count_att23 =r(N)
local y1mark = r(mean)*100


	post `attrition' ("\quad Pooled male")  (`count_all') (`attrit') (`count_att') (`change') (`count_att23') (`y1mark')

	

***** MI subjects	

	
post `attrition' ("\textit{MI subjects}")  (.) (.) (.) (.) (.) (.)
		
su attrit if type==1 & EEM==1
local count_all = r(N)
local attrit = r(mean)*100
		
su changed if type==1 & attrit==1 & EEM==1
local count_att = r(N)
local change = r(mean)*100

su  ns_std_perc_y1 if type==1 & attrit==1 & EEM==1
local count_att23 =r(N)
local y1mark = r(mean)*100

	post `attrition' ("\quad Directly admitted female")  (`count_all') (`attrit') (`count_att') (`change') (`count_att23') (`y1mark')


* Pooled male

su attrit if type==2 & EEM==1
local count_all = r(N)
local attrit = r(mean)*100
		
su changed if type==2 & attrit==1 & EEM==1
local count_att = r(N)
local change = r(mean)*100

su  ns_std_perc_y1 if type==2 & attrit==1 & EEM==1
local count_att23 =r(N)
local y1mark = r(mean)*100

post `attrition' ("\quad Pooled male")  (`count_all') (`attrit') (`count_att') (`change') (`count_att23') (`y1mark')

postclose `attrition'


********************************************************************************

*		Printing Tables C1 and C3

********************************************************************************

use `attrited', clear
format attrit %9.0fc
format change %9.0fc
format y1mark %9.1fc

listtab * using "${tables}attrition_test.tex", ///
    rstyle(tabular) replace ///
    head("\begin{tabular}{@{\extracolsep{2pt}} l c c c c c c D{.}{.}{-3} D{.}{.}{-3}}" ///
    "\toprule" ///
    " & \multicolumn{2}{c}{All students} & \multicolumn{2}{c}{Attrited students} & \multicolumn{2}{c}{Attrited in years 2 or 3} \\" ///
	"\cmidrule(lr){2-3}\cmidrule(lr){4-5}\cmidrule(lr){6-7}" ///
"& N & \% attrit & N & \% subject change & N & Year 1 performance \\  \midrule" "& (1) & (2) & (3) & (4) & (5) & (6) \\ \midrule") ///
    foot("\bottomrule" "\end{tabular}")

use `stats', clear
format count %9.0fc
format share %9.0fc

listtab * using "${tables}sample_test.tex", ///
    rstyle(tabular) replace ///
    head("\begin{tabular}{@{\extracolsep{2pt}} l c  D{.}{.}{-3} D{.}{.}{-3}}" ///
    "\toprule" ///
"& N & \%  \\  \midrule" ) ///
    foot("\bottomrule" "\end{tabular}")
